﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace SuporeSliplampApp.Util
{
    public class SerialPortHelper
    {
        public static SerialPort serialPort = null;
        static string End = Environment.NewLine;
        static bool isOpen = false;
        public static string receivedValue = string.Empty;
        public static bool isPhoto = false;
        public static string eyeFlg = "0";

        public SerialPortHelper()
        {
            string[] ports = SerialPort.GetPortNames();
        }


        public static bool ConnectPort()
        {
            string[] ports = SerialPort.GetPortNames();
            string bauRate = "94600";
            bool res = false;
            foreach (string port in ports) 
            {
                
                serialPort = new SerialPort(port,38400, Parity.None, 8, StopBits.One);
                serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                try
                { 
                    serialPort.Open();
                    res = true;
                    isOpen = true;
                    break;
                    
                }
                catch (Exception ex) 
                {
                    res = false;
                }
            }
            return res;
            
    }

        public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string data = sp.ReadExisting();
            string s = string.Empty;
            byte[] encodedBytes = Encoding.UTF8.GetBytes(data);
            for (int i = 0; i < encodedBytes.Length; i++)
            {
                receivedValue = receivedValue + encodedBytes[i];

                   // receivedValue += encodedBytes[i];

            }
            switch (receivedValue)
            {
                case "6320000263":
                    SerialPortHelper.eyeFlg = "0";
                        break;
                case "6320010263":
                    SerialPortHelper.eyeFlg = "1";
                    break;
                case "6330000363":
                    SerialPortHelper.isPhoto = false;
                    break;
                case "6330010363":
                    SerialPortHelper.isPhoto = true;
                    break;
                default:
                    SerialPortHelper.eyeFlg = "0";
                    SerialPortHelper.isPhoto = false;
                    break;


            }
        }

        public static byte[] ConvertHexStringToBytes(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            byte[] returnBytes = new byte[hexString.Length / 2];
            if (hexString.Length % 2 == 0)
            {
                for (int i = 0; i < returnBytes.Length; i++)
                {
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                }
            }
            return returnBytes;
        }

        public static void closePort()
        {

            try
            {
                if (serialPort != null)
                {
                    if (serialPort.IsOpen)
                        serialPort.Close();
                    serialPort.Dispose();
                }
            }
            catch (Exception)
            {

                //throw;
            }
        }

        public static void SengInstructions(byte[] data)
        {
            if (isOpen)
            {
                SerialPortWrite(data);
            }
            else
            {
                //MessageBox.Show("请先连接串口!");
            }
        }

        public static bool SerialPortWrite(byte[] data)
        {
            try
            {
                string res = string.Join(" ", data);
                serialPort.Write(data, 0, data.Length);
                //Dispatcher.Invoke(new Action(() => { sendText.AppendText("发送成功: " + res + End); }));

            }
            catch (Exception ex)
            {
                //Dispatcher.Invoke(new Action(() => { sendText.AppendText("发送失败：" + ex.ToString() + End); }));
                //sendText.AppendText("发送失败：" + ex.ToString() + End);
                return false;
            }
            return true;
        }


    }
}
